package medusa

import (
    "log"
    "math"
)

type Log interface {
    SetLv(lv int)

    Error(heading string, format string, a ...interface{})
    Warning(heading string, format string, a ...interface{})
    Success(heading string, format string, a ...interface{})
    Debug(heading string, format string, a ...interface{})
    Stack(heading string, format string, a ...interface{})
    Temp(heading string, format string, a ...interface{})
}

type mds_log_event struct {
    heading string
    format  string
    args    []interface{}
}

type MedusaLog struct {
    lv       int
    log_chan chan *mds_log_event
}

func NewMedusaLog(lv int) *MedusaLog {
    obj := new(MedusaLog)
    obj.lv = lv
    obj.log_chan = make(chan *mds_log_event, math.MaxInt16)
    go func() {
        for {
            ev := <-obj.log_chan
            if ev == nil {
                break
            }
            log.Printf("["+ev.heading+"] "+ev.format, ev.args...)
        }

    }()

    return obj
}

var default_log *MedusaLog = nil

func DefaultLog() *MedusaLog {
    if default_log == nil {
        default_log = NewMedusaLog(1)
    }
    return default_log
}

func (self *MedusaLog) SetLv(lv int) {
    self.lv = lv
}

func (self *MedusaLog) Error(heading string, format string, args ...interface{}) {
    if self.lv >= 1 {
        self.log_chan <- &(mds_log_event{heading, format, args})
    }
}

func (self *MedusaLog) Warning(heading string, format string, args ...interface{}) {
    if self.lv >= 2 {
        self.log_chan <- &(mds_log_event{heading, format, args})
    }
}

func (self *MedusaLog) Success(heading string, format string, args ...interface{}) {

    if self.lv >= 3 {
        self.log_chan <- &(mds_log_event{heading, format, args})
    }
}

func (self *MedusaLog) Debug(heading string, format string, args ...interface{}) {

    if self.lv >= 4 {
        self.log_chan <- &(mds_log_event{heading, format, args})
    }
}

func (self *MedusaLog) Stack(heading string, format string, args ...interface{}) {

    if self.lv >= 5 {
        self.log_chan <- &(mds_log_event{heading, format, args})
    }
}

func (self *MedusaLog) Temp(heading string, format string, args ...interface{}) {

    if self.lv >= 6 {
        self.log_chan <- &(mds_log_event{heading, format, args})
    }
}
